#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>

#include "idc_ubus_mesh.h"
#include "whqx_mesh_service.h"
#include "yunw_mesh_service.h"

bool isEmpty(const char *str) {
        if (str != NULL) {
                if (strlen(str) == 0) {
                        return true;
                }
                return false;
        }
        return true;
}

int ubus_whqx_mesh_info(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        blob_buf_init(&b, 0);
        
        //todo
        cJSON *cjson = systeminfo();
        char *jsonstring = cJSON_Print(cjson);
        void *tb = blobmsg_open_table(&b, "basicData");
        cJSON *basicData = cJSON_GetObjectItem(cjson, "systeminfo");
        char* modelname = cJSON_GetObjectItem(basicData,"modelname")->valuestring;
        if(!isEmpty(modelname)) blobmsg_add_string(&b, "modelname", modelname);
        
        char* firversion = cJSON_GetObjectItem(basicData,"firversion")->valuestring;
        if(!isEmpty(firversion)) blobmsg_add_string(&b, "firversion", firversion);

        char* loacal_mac = cJSON_GetObjectItem(basicData,"mac")->valuestring;
        if(!isEmpty(loacal_mac)) blobmsg_add_string(&b, "mac", loacal_mac);

        char* runtime = cJSON_GetObjectItem(basicData,"runtime")->valuestring;
        if(!isEmpty(runtime)) blobmsg_add_string(&b, "runtime", runtime);

        char* temperature = cJSON_GetObjectItem(basicData,"temperature")->valuestring;
        if(!isEmpty(temperature)) blobmsg_add_string(&b, "temperature", temperature);

        char* voltage = cJSON_GetObjectItem(basicData,"voltage")->valuestring;
        if(!isEmpty(voltage)) blobmsg_add_string(&b, "voltage", voltage);

        blobmsg_close_table(&b, tb);
        //解析connectList
        cJSON *connectlist = cJSON_GetObjectItem(cjson, "connectlist");
        void *arr = blobmsg_open_array(&b, "connectlist");
        cJSON *item = NULL;
        cJSON_ArrayForEach(item, connectlist) {
                // 获取每个对象的"name"和"age"字段
                char *mac = cJSON_GetObjectItem(item, "mac")->valuestring;
                char *expect = cJSON_GetObjectItem(item, "expect")->valuestring;
                char *ssid = cJSON_GetObjectItem(item, "ssid")->valuestring;
                char *noise = cJSON_GetObjectItem(item, "noise")->valuestring;
                char *rxbit = cJSON_GetObjectItem(item, "rxbit")->valuestring;
                char *acktimeout = cJSON_GetObjectItem(item, "acktimeout")->valuestring;
                char *txbit = cJSON_GetObjectItem(item, "txbit")->valuestring;
                char *signal = cJSON_GetObjectItem(item, "signal")->valuestring;
                // 打印获取到的数据
                void *node = blobmsg_open_table(&b, NULL);
                if(!isEmpty(mac)) blobmsg_add_string(&b, "mac", mac);
                if(!isEmpty(expect)) blobmsg_add_string(&b, "expect", expect);
                if(!isEmpty(ssid)) blobmsg_add_string(&b, "ssid", ssid);
                if(!isEmpty(noise)) blobmsg_add_string(&b, "noise", noise);
                if(!isEmpty(rxbit)) blobmsg_add_string(&b, "rxbit", rxbit);
                if(!isEmpty(acktimeout)) blobmsg_add_string(&b, "acktimeout", acktimeout);
                if(!isEmpty(txbit)) blobmsg_add_string(&b, "txbit", txbit);
                if(!isEmpty(signal)) blobmsg_add_string(&b, "signal", signal);
                blobmsg_close_table(&b, node);
        }
        printf("5\n");
        blobmsg_close_array(&b, arr);
        ubus_send_reply(ctx, req, b.head);
        
        return 0;
} 
int ubus_whqx_mesh_reboot(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        reboot();
        return 0;
}  
int ubus_whqx_mesh_reset(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        reset();     
        return 0;
}  

int ubus_yunw_mesh_status(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg)
{
        blob_buf_init(&b, 0);
        
        //todo
        curl_init();
        login(meshCfg.username, meshCfg.password);
        char *cjson_str = sysstatus();
        clean_curl();
        cJSON *root = cJSON_Parse(cjson_str);
        if (root != NULL) {
                cJSON *o_version = cJSON_GetObjectItem(root,"version");
                if (o_version != NULL)
                {
                        char* version = o_version->valuestring;
                        if(!isEmpty(version)) blobmsg_add_string(&b, "version", version);
                }
                
                cJSON *csw2205 = cJSON_GetObjectItem(root, "csw2205");  //CSW520A
                if(csw2205 !=NULL) {
                        cJSON *o_iface = cJSON_GetObjectItem(csw2205,"iface");
                        if(o_iface != NULL){
                                char* iface = o_iface->valuestring;
                                if(!isEmpty(iface)) blobmsg_add_string(&b, "iface", iface);
                        }
                        cJSON *o_mac = cJSON_GetObjectItem(csw2205,"mac");
                        if(o_mac != NULL){
                                char* mac = o_mac->valuestring;
                                if(!isEmpty(mac)) blobmsg_add_string(&b, "mac", mac);
                        }
                        cJSON *o_ip = cJSON_GetObjectItem(csw2205,"ip");
                        if(o_ip != NULL){
                                char* ip = o_ip->valuestring;
                                if(!isEmpty(ip)) blobmsg_add_string(&b, "ip", ip);
                        }
                        cJSON *o_wifimode = cJSON_GetObjectItem(csw2205,"wifimode");
                        if(o_wifimode != NULL){
                                int wifimode = o_wifimode->valueint;
                                blobmsg_add_u32(&b, "wifimode", wifimode);
                        }
                        cJSON *o_channel = cJSON_GetObjectItem(csw2205,"channel");
                        if(o_channel != NULL){
                                int channel = o_channel->valueint;
                                blobmsg_add_u32(&b, "channel", channel);
                        }
                        cJSON *o_freq = cJSON_GetObjectItem(csw2205,"freq");
                        if(o_freq != NULL){
                                int freq = o_freq->valueint;
                                blobmsg_add_u32(&b, "freq", freq);
                        }
                        
                        cJSON *rxinfomsgs = cJSON_GetObjectItem(csw2205, "rxinfomsgs");  //CSW520A
                        if(rxinfomsgs != NULL) {
                                void *arr1 = blobmsg_open_array(&b, "rxinfomsgs");
                                cJSON *item = NULL;
                                cJSON_ArrayForEach(item, rxinfomsgs) {
                                        void *node = blobmsg_open_table(&b, NULL);
                                        cJSON * o_mac = cJSON_GetObjectItem(item,"mac");
                                        if(o_mac != NULL){
                                                 char* item_mac = o_mac->valuestring;
                                                if(!isEmpty(item_mac)) blobmsg_add_string(&b, "mac", item_mac);
                                        }
                                        cJSON *o_rssi0 = cJSON_GetObjectItem(item,"rssi0");
                                        if(o_rssi0 != NULL){
                                                int rssi0 = o_rssi0->valueint;
                                                blobmsg_add_u32(&b, "rssi0", rssi0);
                                        }
                                        cJSON *o_rssi1 = cJSON_GetObjectItem(item,"rssi1");
                                        if(o_rssi1 != NULL){
                                                int rssi1 = o_rssi1->valueint;
                                                blobmsg_add_u32(&b, "rssi1", rssi1);
                                        }
                                        cJSON *o_quality = cJSON_GetObjectItem(item,"quality");
                                        if(o_quality != NULL){
                                                int quality = o_quality->valueint;
                                                blobmsg_add_u32(&b, "quality", quality);
                                        }
                                        cJSON * o_rate = cJSON_GetObjectItem(item,"rate");
                                        if(o_rate != NULL){
                                                 char* rate = o_rate->valuestring;
                                                if(!isEmpty(rate)) blobmsg_add_string(&b, "rate", rate);
                                        }
                                        
                                        blobmsg_close_table(&b, node);
                                }
                                blobmsg_close_array(&b, arr1);
                                // if(item != NULL) cJSON_Delete(item);
                        }
                        
                        cJSON *meshmpath = cJSON_GetObjectItem(csw2205, "meshmpath");  //CSW520A
                        if(meshmpath != NULL) {
                                void *arr2 = blobmsg_open_array(&b, "meshmpath");
                                cJSON *item = NULL;
                                cJSON_ArrayForEach(item, meshmpath) {
                                        void *node = blobmsg_open_table(&b, NULL);
                                        cJSON * o_dstmac = cJSON_GetObjectItem(item,"dstmac");
                                        if(o_dstmac != NULL){
                                                 char* dstmac = o_dstmac->valuestring;
                                                if(!isEmpty(dstmac)) blobmsg_add_string(&b, "dstmac", dstmac);
                                        }
                                        cJSON * o_dstip = cJSON_GetObjectItem(item,"dstip");
                                        if(o_dstip != NULL){
                                                 char* dstip = o_dstip->valuestring;
                                                if(!isEmpty(dstip)) blobmsg_add_string(&b, "dstip", dstip);
                                        }
                                        cJSON * o_nextmac = cJSON_GetObjectItem(item,"nextmac");
                                        if(o_nextmac != NULL){
                                                 char* nextmac = o_nextmac->valuestring;
                                                if(!isEmpty(nextmac)) blobmsg_add_string(&b, "nextmac", nextmac);
                                        }
                                        cJSON * o_nextip = cJSON_GetObjectItem(item,"nextip");
                                        if(o_nextip != NULL){
                                                 char* nextip = o_nextip->valuestring;
                                                if(!isEmpty(nextip)) blobmsg_add_string(&b, "nextip", nextip);
                                        }
                                        
                                        blobmsg_close_table(&b, node);
                                }
                                blobmsg_close_array(&b, arr2);
                                // if(item != NULL) cJSON_Delete(item);
                        }
                        
                }
                cJSON *csw2206 = cJSON_GetObjectItem(root, "csw2206");  //CSW620A
                if(csw2206 !=NULL) {
                        cJSON *o_iface = cJSON_GetObjectItem(csw2206,"iface");
                        if(o_iface != NULL){
                                char* iface = o_iface->valuestring;
                                if(!isEmpty(iface)) blobmsg_add_string(&b, "iface", iface);
                        }
                        cJSON *o_mac = cJSON_GetObjectItem(csw2206,"mac");
                        if(o_mac != NULL){
                                char* mac = o_mac->valuestring;
                                if(!isEmpty(mac)) blobmsg_add_string(&b, "mac", mac);
                        }
                        cJSON *o_ip = cJSON_GetObjectItem(csw2206,"ip");
                        if(o_ip != NULL){
                                char* ip = o_ip->valuestring;
                                if(!isEmpty(ip)) blobmsg_add_string(&b, "ip", ip);
                        }
                        cJSON *o_wifimode = cJSON_GetObjectItem(csw2206,"wifimode");
                        if(o_wifimode != NULL){
                                int wifimode = o_wifimode->valueint;
                                blobmsg_add_u32(&b, "wifimode", wifimode);
                        }
                        cJSON *o_channel = cJSON_GetObjectItem(csw2206,"channel");
                        if(o_channel != NULL){
                                int channel = o_channel->valueint;
                                blobmsg_add_u32(&b, "channel", channel);
                        }
                        cJSON *o_freq = cJSON_GetObjectItem(csw2206,"freq");
                        if(o_freq != NULL){
                                int freq = o_freq->valueint;
                                blobmsg_add_u32(&b, "freq", freq);
                        }
                        
                        cJSON *rxinfomsgs = cJSON_GetObjectItem(csw2206, "rxinfomsgs");  //CSW520A
                        if(rxinfomsgs != NULL) {
                                void *arr1 = blobmsg_open_array(&b, "rxinfomsgs");
                                cJSON *item = NULL;
                                cJSON_ArrayForEach(item, rxinfomsgs) {
                                        void *node = blobmsg_open_table(&b, NULL);
                                        cJSON * o_mac = cJSON_GetObjectItem(item,"mac");
                                        if(o_mac != NULL){
                                                 char* item_mac = o_mac->valuestring;
                                                if(!isEmpty(item_mac)) blobmsg_add_string(&b, "mac", item_mac);
                                        }
                                        cJSON *o_rssi0 = cJSON_GetObjectItem(item,"rssi0");
                                        if(o_rssi0 != NULL){
                                                int rssi0 = o_rssi0->valueint;
                                                blobmsg_add_u32(&b, "rssi0", rssi0);
                                        }
                                        cJSON *o_rssi1 = cJSON_GetObjectItem(item,"rssi1");
                                        if(o_rssi1 != NULL){
                                                int rssi1 = o_rssi1->valueint;
                                                blobmsg_add_u32(&b, "rssi1", rssi1);
                                        }
                                        cJSON *o_quality = cJSON_GetObjectItem(item,"quality");
                                        if(o_quality != NULL){
                                                int quality = o_quality->valueint;
                                                blobmsg_add_u32(&b, "quality", quality);
                                        }
                                        cJSON * o_rate = cJSON_GetObjectItem(item,"rate");
                                        if(o_rate != NULL){
                                                 char* rate = o_rate->valuestring;
                                                if(!isEmpty(rate)) blobmsg_add_string(&b, "rate", rate);
                                        }
                                        
                                        blobmsg_close_table(&b, node);
                                }
                                blobmsg_close_array(&b, arr1);
                                // if(item != NULL) cJSON_Delete(item);
                        }
                        
                        cJSON *meshmpath = cJSON_GetObjectItem(csw2206, "meshmpath");  //CSW520A
                        if(meshmpath != NULL) {
                                void *arr2 = blobmsg_open_array(&b, "meshmpath");
                                cJSON *item = NULL;
                                cJSON_ArrayForEach(item, meshmpath) {
                                        void *node = blobmsg_open_table(&b, NULL);
                                        cJSON * o_dstmac = cJSON_GetObjectItem(item,"dstmac");
                                        if(o_dstmac != NULL){
                                                 char* dstmac = o_dstmac->valuestring;
                                                if(!isEmpty(dstmac)) blobmsg_add_string(&b, "dstmac", dstmac);
                                        }
                                        cJSON * o_dstip = cJSON_GetObjectItem(item,"dstip");
                                        if(o_dstip != NULL){
                                                 char* dstip = o_dstip->valuestring;
                                                if(!isEmpty(dstip)) blobmsg_add_string(&b, "dstip", dstip);
                                        }
                                        cJSON * o_nextmac = cJSON_GetObjectItem(item,"nextmac");
                                        if(o_nextmac != NULL){
                                                 char* nextmac = o_nextmac->valuestring;
                                                if(!isEmpty(nextmac)) blobmsg_add_string(&b, "nextmac", nextmac);
                                        }
                                        cJSON * o_nextip = cJSON_GetObjectItem(item,"nextip");
                                        if(o_nextip != NULL){
                                                 char* nextip = o_nextip->valuestring;
                                                if(!isEmpty(nextip)) blobmsg_add_string(&b, "nextip", nextip);
                                        }
                                        
                                        blobmsg_close_table(&b, node);
                                }
                                blobmsg_close_array(&b, arr2);
                                // if(item != NULL) cJSON_Delete(item);
                        }
                        
                }
                // cJSON_Delete(root);
        }
        ubus_send_reply(ctx, req, b.head);
        
        return 0;
} 



